package cn.sciento.farm.devicemanager.gateway.uplink.service.sensor;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.common.constant.RedisConstant;
import cn.sciento.farm.devicemanager.common.util.RedisUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.gateway.model.uplink.CollectionData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.ControlData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.DeviceSystemPacket;
import cn.sciento.farm.devicemanager.gateway.model.uplink.SensorConfig;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import cn.sciento.farm.devicemanager.service.DeviceService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author wumu
 * @version V1.0
 * @date 7/20/18
 * @time 2:49 PM
 */
@Slf4j
@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_SENSOR)
public class SensorDataAdapter extends AbstractStringAdapter {

    @Autowired
    private MessageAllDisposeService messageAllDisposeService;

    @Autowired
    protected ContextTransferService contextTransferService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public void analyseStringData(String[] subTopic, String message) {
        if (subTopic.length > 5 || subTopic.length < 4) {
            return;
        }
        log.info("适配器  topic  {}  msg   {}", Arrays.toString(subTopic),message);

        String type = subTopic[1];
        String concentrator = subTopic[3];
        switch (type) {
            case MqttConstant.UP:
                if (MqttConstant.COL_TYPE.equals(subTopic[2])) {
                    List<CollectionData> deviceDataList = contextTransferService.regularData(subTopic[3], null, message);
//                    log.info("处理传感器的数据 {}",deviceDataList.toString());
                    for (CollectionData deviceData : deviceDataList) {
                        messageAllDisposeService.changeDeviceData(deviceData);
                    }
                    String deviceStr = RedisUtil.getCacheStr(RedisConstant.CONCENTRATOR_CACHE + concentrator);
                    DevDevice device = JSON.parseObject(deviceStr, DevDevice.class);
                    if (null != device) {
                        if (device.getOnline() != 1) {
                            deviceService.updateOnline(concentrator, 1);
                        }
                    }
                } else if (MqttConstant.SWITCH_TYPE.equals(subTopic[2])) {
                    List<ControlData> switchStatuses = contextTransferService.regularSwitch(subTopic[3], null, message);
                    for (ControlData switchStatus : switchStatuses) {
                        messageAllDisposeService.changeSwitch(switchStatus);
                    }
                } else if (MqttConstant.SYS.equals(subTopic[2])) {
                    DeviceSystemPacket deviceSystemPacket = JSON.parseObject(message, DeviceSystemPacket.class);
                    if (deviceSystemPacket == null) {
                        return;
                    }
                    if(deviceSystemPacket.getvBat() != null) {
                        deviceSystemPacket.setvBat(deviceSystemPacket.getvBat()*0.001f);
                    }
                    deviceSystemPacket.setEui(concentrator);
                    messageAllDisposeService.changeSystem(deviceSystemPacket);

                }
                break;
            case MqttConstant.DOWN:

                break;
            case MqttConstant.OFFLINE:
                Map map = JSON.parseObject(message, Map.class);
                if (map == null) {
                    return;
                }
                if (MqttConstant.SYS.equals(subTopic[2])) {
                    String offline = "" + map.get("will");
                    if (StringUtils.isEmpty(offline)) {
                        break;
                    }
                    if (MqttConstant.OFFLINE.equals(offline)) {
                        deviceService.updateOnline(concentrator, 0);
                    }
                    break;
                }
                String node = "" + map.get("id");
                if (!StringUtils.isEmpty(node)) {
                    if (MqttConstant.COL_TYPE.equals(subTopic[2])) {
                        messageAllDisposeService.changeCollectionStatus(concentrator, node, 0);
                        break;
                    } else {
                        messageAllDisposeService.changeSwitchStatus(concentrator, node, 0);
                        break;
                    }
                }
                break;
            default:
                break;
        }
    }
}
